package org.game.manager;

import com.google.common.collect.Maps;
import com.sojoys.artifact.manager.MessageManager;
import com.sojoys.artifact.tools.ToolDate;
import org.cfg.BuildCfg;
import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.constant.smallkey.WorkLogSmallKey;
import org.game.constant.BuildingConstant;
import org.game.db.bean.Building;
import org.game.db.bean.City;
import org.game.db.bean.Player;
import org.game.db.cache.CityCache;
import org.game.log.LogBuilding;
import org.game.log.builder.LogDsp;
import org.game.log.dsp.BuildingDsp;
import org.game.module.buildingbo.BuildingBO;
import org.game.msgBuilder.BuildingMsgBuilder;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventPlugin;
import org.game.protobuf.s2c.S2CBuildingMsg;
import org.game.protobuf.s2c.S2CBuildingMsg.UpdateBuildingInfo;
import org.game.util.ProtoUnit;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 城建管理器
 * Created by fxf on 2017/9/7.
 */
public class BuildingManager {

    private BuildingManager() {
    }

    /**
     * 建筑升级
     *
     * @param player   玩家对象
     * @param building 建筑
     */
    public static void buildingUpgrade(Player player, Building building) {
    	int oldLvl = building.getBuildingProxy().getCfg().getLevel();
        BuildCfg nextLevelCfg = building.getBuildingProxy().getNextLevelCfg();
        building.setFinishTime(null);
        building.setState(BuildingConstant.BUIDING_STATE_UPGRADE_FINISH);
        building.setCid(nextLevelCfg.getId());
        building.update();
        
        Map<String, Object> in = Maps.newHashMap();
        in.put(EventConditionKey.CONDITION_TYPE, EventConditionType.BUILDING_LVL_COUNT);
        in.put(EventConditionKey.BUILD_CID, nextLevelCfg.getBuildId());
		in.put(EventConditionKey.OLD_LEVEL, oldLvl);
		in.put(EventConditionKey.NOW_LEVEL, nextLevelCfg.getLevel());
		GameEvent event = new GameEvent(player, EventType.BUILDING_EVENT, in);
		GameEventPlugin.syncSubmit(event);
        
        UpdateBuildingInfo updateBuildingInfo = BuildingMsgBuilder.buildUpdateBuildingInfoMsg(building,S2CBuildingMsg.BuildingEvent.UPGRADE_FINISH);
        player.getPlayerProxy().sendMsg(MessageManager.me().create(BuildingBO.updateBuilding, ProtoUnit.toByte(updateBuildingInfo)));
        LogDsp logDsp = LogDsp.newBuilder(BuildingDsp.FINISH_UPGRADE).addAttr("oldLvl",oldLvl).addAttr("nowLvl", nextLevelCfg.getLevel());
        LogBuilding.me().log(player.getId(),nextLevelCfg.getBuildId(), logDsp);
    }

    /**
     * 打工完成
     *
     * @param player   玩家对象
     * @param building 建筑
     */
    public static void workFinish(Player player, Building building) {
        building.setFinishTime(null);
        building.setNextRewardTime(null);
        building.setState(BuildingConstant.BUIDING_STATE_WORK_FINISH);

        //添加打工完成日志
        Map<Integer, Object> log = Maps.newHashMap();
		log.put(WorkLogSmallKey.TYPE, 3);
		log.put(WorkLogSmallKey.TIME, (int) (System.currentTimeMillis() / ToolDate.TIME_SECOND));
		building.getEventLogs().add(log);
		building.update();
		BuildingMsgBuilder.pushWorkLog(player, building.getCid(), log);
        
		int workTime = building.getBuildingProxy().getCfg().getWorkTime();
		Map<String, Object> in = Maps.newHashMap();
		in.put(EventConditionKey.CONDITION_TYPE, EventConditionType.WORK_TIME);
        in.put(EventConditionKey.BUILD_CID, building.getBuildingProxy().getBuildId());
        in.put(EventConditionKey.WORK_TIME, workTime);
		GameEvent event = new GameEvent(player, EventType.BUILDING_EVENT, in);
		GameEventPlugin.syncSubmit(event);
		
        UpdateBuildingInfo updateBuildingInfo = BuildingMsgBuilder.buildUpdateBuildingInfoMsg(building,S2CBuildingMsg.BuildingEvent.WORK_FINISH);
        player.getPlayerProxy().sendMsg(MessageManager.me().create(BuildingBO.updateBuilding, ProtoUnit.toByte(updateBuildingInfo)));
    }

    /**
     * 获取所有已经解锁的建筑cid
     */
    public static List<Integer> getAllUnlockedBuildingCid(Player player){
        List<Building> buildingList = player.getBuildingsFkPlayerId();
        return buildingList.stream().map(building -> building.getBuildingProxy().getCfg().getBuildId())
                .collect(Collectors.toList());
    }

    public static void unlockCity(Player player,Building building){
        int cityCid =  building.getBuildingProxy().getCfg().getCityId();
        City city = CityCache.me().getByCidPlayerId(cityCid,player.getId());
        if (city == null){
            city = new City(0l,cityCid,false,player.getId());
            city.insert();
        }
    }
}
